package ms.unicorn.diagram.edit.parts;

import java.util.Map;

import ms.unicorn.Element;
import ms.unicorn.diagram.edit.policies.RelationItemSemanticEditPolicy;
import ms.unicorn.modellanguage.LanguageElement;
import ms.unicorn.modellanguage.LanguageRegistry;
import ms.unicorn.modellanguage.LanguageRelation;

import org.eclipse.draw2d.Connection;
import org.eclipse.draw2d.IFigure;
import org.eclipse.gef.EditPart;
import org.eclipse.gmf.runtime.diagram.ui.editparts.ConnectionNodeEditPart;
import org.eclipse.gmf.runtime.diagram.ui.editparts.ITreeBranchEditPart;
import org.eclipse.gmf.runtime.diagram.ui.editpolicies.EditPolicyRoles;
import org.eclipse.gmf.runtime.draw2d.ui.figures.PolylineConnectionEx;
import org.eclipse.gmf.runtime.draw2d.ui.figures.WrappingLabel;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.swt.SWT;

/**
 * @generated
 */
public class RelationEditPart extends ConnectionNodeEditPart implements
		ITreeBranchEditPart {

	/**
	 * @generated
	 */
	public static final int VISUAL_ID = 4001;

	/**
	 * @generated
	 */
	public RelationEditPart(View view) {
		super(view);
	}

	/**
	 * @generated
	 */
	protected void createDefaultEditPolicies() {
		super.createDefaultEditPolicies();
		installEditPolicy(EditPolicyRoles.SEMANTIC_ROLE,
				new RelationItemSemanticEditPolicy());
	}

	/**
	 * @generated
	 */
	protected boolean addFixedChild(EditPart childEditPart) {
		/*if (childEditPart instanceof RelationElementTypeEditPart) {
			((RelationElementTypeEditPart) childEditPart)
					.setLabel(getPrimaryShape().getFigureRelationNameFigure());
			return true;
		}*/
		return false;
	}

	/**
	 * @generated
	 */
	protected void addChildVisual(EditPart childEditPart, int index) {
		if (addFixedChild(childEditPart)) {
			return;
		}
		super.addChildVisual(childEditPart, index);
	}

	/**
	 * @generated
	 */
	protected boolean removeFixedChild(EditPart childEditPart) {
		if (childEditPart instanceof RelationElementTypeEditPart) {
			return true;
		}
		return false;
	}

	/**
	 * @generated
	 */
	protected void removeChildVisual(EditPart childEditPart) {
		if (removeFixedChild(childEditPart)) {
			return;
		}
		super.removeChildVisual(childEditPart);
	}

	/**
	 * Creates figure for this edit part.
	 * 
	 * Body of this method does not depend on settings in generation model
	 * so you may safely remove <i>generated</i> tag and modify it.
	 * 
	 * @generated NOT
	 */

	protected Connection createConnectionFigure() {
		Element element = (Element) resolveSemanticElement();
		LanguageElement lang = LanguageRegistry.INSTANCE.getLanguageElement(element);
		
		if (lang instanceof LanguageRelation) {
			Map<String, IFigure> figures = lang.getEditHelperAdvice().createFigures(null, element, this);
			if (figures != null) {
				IFigure primaryShape = figures.get("connection");
				return (Connection) primaryShape;
			}
				
		}
		return new RelationFigure();
	}

	/**
	 * @generated NOT
	 */
	public IFigure getPrimaryShape() {
		return (IFigure) getFigure();
	}

	/**
	 * @generated
	 */
	public class RelationFigure extends PolylineConnectionEx {

		/**
		 * @generated
		 */
		private WrappingLabel fFigureRelationNameFigure;

		/**
		 * @generated
		 */
		public RelationFigure() {
			createContents();
		}

		/**
		 * @generated
		 */
		private void createContents() {

			fFigureRelationNameFigure = new WrappingLabel();
			fFigureRelationNameFigure.setText("<...>");

			this.add(fFigureRelationNameFigure);
			this.lineStyle = SWT.LINE_DASH;
		}

		/**
		 * @generated
		 */
		public WrappingLabel getFigureRelationNameFigure() {
			return fFigureRelationNameFigure;
		}

	}

}
